package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.ApplyTcDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.ApplyTcInfo;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.webUtil;

public class LoadApplyTc extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		LogUtil.print("loadmyapply");
		String token = webUtil.getCookie(request, "LoginCookie");
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			Repost.dberror(request, response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		if (info==null) {
			Repost.nologin(response);
			return;
		}
		
		if (info.getUlevel()>=Constant.ULEVEL_teacher) {
			Repost.message("你已是教师，无需申请！", request, response);
			return;
		}
		
		ApplyTcDao adao = dao.getApplyTcDao();
		ApplyTcInfo ainfo = adao.getapplyinfo(info.getId());
		
		if (ainfo==null) {
			Repost.success("applyteacher.jsp", request, response);
		} else {
			
			if (ainfo.getResult() == Constant.APPLYTC_applying) {
				Repost.message("你的申请正在核审，请等待1-5个工作日！", request, response);
			} else if (ainfo.getResult()== Constant.APPLYTC_fail) {
				request.setAttribute("data", ainfo);
				request.setAttribute("message", ainfo.getResultcontent());
				Repost.success("applyteacher.jsp", request, response);
			} else {
				Repost.message("你已是教师，无需申请！", request, response);
			}
		}
		
	}

}
